package edu.xhu;

import java.util.ArrayList;

import com.bean.JobBean;
import com.constant.Constant;
import com.data.CDJobData;
import com.data.JobData;
import com.data.MoreJobData;
import com.net.ClientError;
import com.net.LoadData;
import com.net.RequestListener;
import com.net.RequestParameter;

import edu.xhu.adapters.ChuanDaJobAdapter;
import edu.xhu.adapters.EmploymentAdapter;
import edu.xhu.view.LoadingDialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 就业模块
 * @author swerit
 *
 */
public class EmploymentActivity extends Activity {
	
	private LinearLayout mContainer;
	private TextView mCurrent, mMore, mChuanDa;
	private LayoutInflater inflater;
	private Button mBtnBack;					//返回按钮
	
	private LinearLayout mCurrentLayout, mMoreLayout, mChuanDaLayout;
	private ListView mCurrentInfoListView, mMoreInfoListView, mChuanDaListView;
	private EmploymentAdapter mCurrentAdapter, mMoreAdapter;
	private ChuanDaJobAdapter mChuanDaJobAdapter;		//川大招聘信息的适配器
	
	private ArrayList<JobBean> mCurrentJobList, mMoreJobList, mChuanDaJobList;
	
	//更多招聘信息界面的页面指针，从1开始
	private int nMorePageIndex = 1;
	private int nLoadedCount = 0;						//已经加载到本地的总数
	private int nTotalCount = 0;						//总共查找结果
	private RelativeLayout buttonLoadNext;				//加载下一页数据的按钮
	private RelativeLayout loadingBar;					//正在加载中显示的进度条
	private View footer, footerCd;
	
	//川大招聘信息已经加载的数量
	private int nChuanDaCount = 0;
	//川大招聘信息的总共数量
	private int nChuanDaTotal = 0;
	//川大界面中的加载更多界面显示控件
	private RelativeLayout buttonLoadNextCD;				//加载下一页数据的按钮
	private RelativeLayout loadingBarCD;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.employment);
		
		initView();
		setListener();
		loadCurrentInfo();
	}

	private void initView(){
		mContainer				= (LinearLayout) findViewById(R.id.employment_container);
		inflater				= LayoutInflater.from(this);
		mBtnBack				= (Button) findViewById(R.id.button_back);
		mCurrent				= (TextView) findViewById(R.id.textview_current);
		mMore					= (TextView) findViewById(R.id.textview_more);
		mChuanDa				= (TextView) findViewById(R.id.textview_chuanda);
		
		mCurrentLayout			= (LinearLayout) inflater.inflate(R.layout.employment_listview, null);
		mMoreLayout				= (LinearLayout) inflater.inflate(R.layout.employment_listview, null);
		mChuanDaLayout			= (LinearLayout) inflater.inflate(R.layout.employment_listview, null);
		
		mCurrentInfoListView	= (ListView) mCurrentLayout.findViewById(R.id.listview_employment);
		mMoreInfoListView		= (ListView) mMoreLayout.findViewById(R.id.listview_employment);
		mChuanDaListView		= (ListView) mChuanDaLayout.findViewById(R.id.listview_employment);
		footer			   	    = LayoutInflater.from(this).inflate(R.layout.listview_footer, null);
		buttonLoadNext		    = (RelativeLayout) footer.findViewById(R.id.button_load_next);
		loadingBar			    = (RelativeLayout) footer.findViewById(R.id.progressbar_loading);
		footerCd				= LayoutInflater.from(this).inflate(R.layout.listview_footer, null);
		buttonLoadNextCD	    = (RelativeLayout) footerCd.findViewById(R.id.button_load_next);
		loadingBarCD		    = (RelativeLayout) footerCd.findViewById(R.id.progressbar_loading);
		
		mMoreInfoListView.addFooterView(footer);
		mMoreLayout.setVisibility(View.GONE);
		mChuanDaLayout.setVisibility(View.GONE);
		mChuanDaListView.addFooterView(footerCd);
		
		mContainer.addView(mCurrentLayout);
		mContainer.addView(mMoreLayout);
		mContainer.addView(mChuanDaLayout);
	}
	
	private void setListener(){
		mBtnBack.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//finish();
				
				Intent intent = new Intent();
				intent.setClass(EmploymentActivity.this,XiHuaActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
		
		mCurrentInfoListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(EmploymentActivity.this, WebviewActivity.class);
				intent.putExtra(WebviewActivity.REQUEST_URL, mCurrentJobList.get(position).url);
				intent.putExtra(WebviewActivity.REFERER, mCurrentJobList.get(position).referer);
				startActivity(intent);
			}
		});
		
		mMoreInfoListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(EmploymentActivity.this, WebviewActivity.class);
				intent.putExtra(WebviewActivity.REQUEST_URL, mMoreJobList.get(position).url);
				intent.putExtra(WebviewActivity.REFERER, mMoreJobList.get(position).referer);
				startActivity(intent);
			}
		});
		
		mChuanDaListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(EmploymentActivity.this, WebviewActivity.class);
				intent.putExtra(WebviewActivity.REQUEST_URL, mChuanDaJobList.get(position).url);
				intent.putExtra(WebviewActivity.REFERER, mChuanDaJobList.get(position).referer);
				startActivity(intent);
			}
		});
		
		mCurrent.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (mCurrentLayout.getVisibility() == View.VISIBLE)
					return;
				
				mCurrent.setBackgroundResource(R.drawable.btn3_left_hot);
				mMore.setBackgroundResource(R.drawable.btn3_mid);
				mChuanDa.setBackgroundResource(R.drawable.btn3_right);
				mCurrentLayout.setVisibility(View.VISIBLE);
				mMoreLayout.setVisibility(View.GONE);
				mChuanDaLayout.setVisibility(View.GONE);
			}
		});
		
		mMore.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (mMoreLayout.getVisibility() == View.VISIBLE)
					return;
				
				mCurrent.setBackgroundResource(R.drawable.btn3_left);
				mMore.setBackgroundResource(R.drawable.btn3_mid_hot);
				mChuanDa.setBackgroundResource(R.drawable.btn3_right);
				mMoreLayout.setVisibility(View.VISIBLE);
				mCurrentLayout.setVisibility(View.GONE);
				mChuanDaLayout.setVisibility(View.GONE);
				
				//没有数据则开始加载数据
				if (null == mMoreJobList){
					loadMoreInfo();
				}
			}
		});
		
		mChuanDa.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mChuanDaLayout.getVisibility() == View.VISIBLE)
					return;
				
				mCurrent.setBackgroundResource(R.drawable.btn3_left);
				mMore.setBackgroundResource(R.drawable.btn3_mid);
				mChuanDa.setBackgroundResource(R.drawable.btn3_right_hot);
				mMoreLayout.setVisibility(View.GONE);
				mCurrentLayout.setVisibility(View.GONE);
				mChuanDaLayout.setVisibility(View.VISIBLE);
				//加载数据
				if (null == mChuanDaJobList){
					loadChuanDaJob();
				}
			}
		});
		
		//西华大学更多界面中的footer
		footer.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (nLoadedCount >= nTotalCount){
					Toast.makeText(EmploymentActivity.this, "已经加载所有数据", 0).show();
					return;
				}
				if (loadingBar.getVisibility() == View.VISIBLE){
					//正在加载则不处理
					return;
				}
				loadingBar.setVisibility(View.VISIBLE);
				buttonLoadNext.setVisibility(View.GONE);
				
				loadMoreInfo();
			}
		});
		
		//川大招聘信息界面中的footer
		footerCd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (nChuanDaCount >= nChuanDaTotal){
					Toast.makeText(EmploymentActivity.this, "已经加载所有数据", 0).show();
					return;
				}
				if (loadingBarCD.getVisibility() == View.VISIBLE){
					//正在加载则不处理
					return;
				}
				loadingBarCD.setVisibility(View.VISIBLE);
				buttonLoadNextCD.setVisibility(View.GONE);
				
				loadChuanDaJob();
			}
		});
	}
	
	/**
	 * 加载当前招聘信息
	 */
	private void loadCurrentInfo(){
		mCurrent.post(new Runnable() {
			public void run() {
				LoadingDialog.showWindow(EmploymentActivity.this);
			}
		});
		
		LoadData.loadData(Constant.JOB_DATA, null, new RequestListener() {
			public void onComplete(Object obj) {
				JobData data = (JobData) obj;
				mCurrentJobList = data.getJobList();
				if (null == mCurrentJobList){
					LoadingDialog.dismiss();
					Toast.makeText(EmploymentActivity.this, "暂时没有招聘信息", 0).show();
					return;
				}
				
				mCurrentAdapter = new EmploymentAdapter(EmploymentActivity.this, mCurrentJobList);
				mCurrentInfoListView.setAdapter(mCurrentAdapter);
				
				LoadingDialog.dismiss();
			}

			public void onError(ClientError ed) {
				LoadingDialog.dismiss();
				ErrorShow.showError(EmploymentActivity.this, ed);
			}
		});
	}
	
	/**
	 * 加载更多招聘信息
	 */
	private void loadMoreInfo(){
		mCurrent.post(new Runnable() {
			public void run() {
				LoadingDialog.showWindow(EmploymentActivity.this);
			}
		});
		
		RequestParameter parameter = new RequestParameter();
		parameter.add("page", "" + nMorePageIndex++);
		
		LoadData.loadData(Constant.MORE_JOB_DATA, parameter, new RequestListener() {
			public void onComplete(Object obj) {
				MoreJobData data = (MoreJobData) obj;
				
				if (null == mMoreJobList){
					mMoreJobList  = data.getJobList();
					nLoadedCount += mMoreJobList.size();
					mMoreAdapter  = new EmploymentAdapter(EmploymentActivity.this, mMoreJobList);
					mMoreInfoListView.setAdapter(mMoreAdapter);
				}else {
					//后面的分页加载处理方法
					ArrayList<JobBean> tmpDataList = data.getJobList();
					mMoreAdapter.addItems(tmpDataList);
					nLoadedCount += tmpDataList.size();
				}
				
				loadEnd();
				
				if (nTotalCount == 0){
					nTotalCount = data.getTotal();
					Toast.makeText(EmploymentActivity.this, "总共: " + nTotalCount + " 条", 0).show();
				}
			}
			public void onError(ClientError ed) {
				nMorePageIndex--;
				loadEnd();
				ErrorShow.showError(EmploymentActivity.this, ed);
			}
		});
	}
	
	/**
	 * 加载川大的招聘信息
	 */
	private void loadChuanDaJob(){
		mCurrent.post(new Runnable() {
			public void run() {
				LoadingDialog.showWindow(EmploymentActivity.this);
			}
		});
		
		RequestParameter parameter = new RequestParameter();
		if (nChuanDaCount == 0)
			parameter.add("start", "");
		else {
			parameter.add("start", nChuanDaCount + "");
		}
		
		LoadData.loadData(Constant.CD_JOB_DATA, parameter, new RequestListener() {
			@Override
			public void onError(ClientError ed) {
				loadEnd();
				ErrorShow.showError(EmploymentActivity.this, ed);
			}
			
			@Override
			public void onComplete(Object obj) {
				CDJobData data = (CDJobData) obj;
				
				if (null == mChuanDaJobList){
					mChuanDaJobList = data.getJobList();
					nChuanDaTotal = data.getTotal();
					Toast.makeText(EmploymentActivity.this, "总共：" + nChuanDaTotal + "条", 0).show();
					nChuanDaCount = mChuanDaJobList.size();
					
					mChuanDaJobAdapter = new ChuanDaJobAdapter(EmploymentActivity.this, mChuanDaJobList);
					mChuanDaListView.setAdapter(mChuanDaJobAdapter);
				}else {
					ArrayList<JobBean> tmpDataList = data.getJobList();
					mChuanDaJobAdapter.addItems(tmpDataList);
					nChuanDaCount += tmpDataList.size();
				}
				
				loadEnd();
			}
		});
	}
	
	private void loadEnd(){
		loadingBar.setVisibility(View.GONE);
		buttonLoadNext.setVisibility(View.VISIBLE);
		loadingBarCD.setVisibility(View.GONE);
		buttonLoadNextCD.setVisibility(View.VISIBLE);
		LoadingDialog.dismiss();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK){
			if (LoadingDialog.isShowing())
				LoadingDialog.dismiss();
			else {
				finish();
				
				/*Intent intent = new Intent();
				intent.setClass(EmploymentActivity.this,XiHuaActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);*/
			}
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
}
